﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMC.Prepare.Core.Domain.Customers;
using IMC.Prepare.Core.Data;

namespace IMC.Prepare.Services.Customers
{
    public partial class CustomerService : ICustomerService
    {
        private readonly IRepository<Customer> _customerRepository;

        public CustomerService(IRepository<Customer> customerRepository)
        {
            _customerRepository = customerRepository;
        }

        #region ICustomerService Members

        /// <summary>
        /// Get customer by username
        /// </summary>
        /// <param name="username">Username</param>
        /// <returns>Customer</returns>
        public virtual Customer GetCustomerByUsername(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
                return null;

            var query = from c in _customerRepository.Table
                        orderby c.Id
                        where c.Username == username
                        select c;
            var customer = query.FirstOrDefault();
            return customer;
        }

        public bool ValidateCustomer(string usernameOrEmail, string password)
        {
            //Customer customer = null;
            ////if (_customerSettings.UsernamesEnabled)
            //    customer = GetCustomerByUsername(usernameOrEmail);
            ////else
            ////    customer = GetCustomerByEmail(usernameOrEmail);

            //if (customer == null || customer.Deleted || !customer.Active)
            //    return false;

            ////only registered can login
            //if (!customer.IsRegistered())
            //    return false;

            //string pwd = string.Empty;
            //switch (customer.PasswordFormat)
            //{
            //    case PasswordFormat.Encrypted:
            //        pwd = _encryptionService.EncryptText(password);
            //        break;
            //    case PasswordFormat.Hashed:
            //        pwd = _encryptionService.CreatePasswordHash(password, customer.PasswordSalt, _customerSettings.HashedPasswordFormat);
            //        break;
            //    default:
            //        pwd = password;
            //        break;
            //}

            //bool isValid = pwd == customer.Password;

            ////save last login date
            //if (isValid)
            //{
            //    customer.LastLoginDateUtc = DateTime.UtcNow;
            //    UpdateCustomer(customer);
            //}
            //else
            //{
            //    customer.FailedPasswordAttemptCount++;
            //    UpdateCustomer(customer);
            //}

            return true;
        }

        #endregion
    }
}
